package lium.lbs.lbsim.fragments

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.support.v4.app.DialogFragment
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import com.buz.hjcdriver.bean.CountryListResultBean
import com.chad.library.adapter.base.BaseQuickAdapter
import com.jakewharton.rxbinding2.widget.RxTextView
import lium.lbs.lbsim.activity.BaseActivity
import com.lmlibrary.bean.ResponseBean
import lium.lbs.lbsim.utils.callbck.DialogCallback
import com.lzy.okgo.model.Response
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_showcontrylist.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.activity.ChangePhoneActivity
import lium.lbs.lbsim.activity.RegistActivity
import lium.lbs.lbsim.adapter.CountryListAdapter
import java.util.HashMap
import java.util.concurrent.TimeUnit

class ShowCountryFragment: DialogFragment(),BaseQuickAdapter.OnItemClickListener {


    var disposable: Disposable?=null

    var baseActivity: BaseActivity?=null
    var adapter:CountryListAdapter?=null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.setCanceledOnTouchOutside(false)
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE) //去掉title
        baseActivity = activity as BaseActivity?
        return inflater.inflate(R.layout.fragment_showcontrylist,null)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initRecyclerView()
        disposable= RxTextView.textChanges(intersome)
                .debounce(200, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .map(CharSequence::toString)
                .subscribe {
                    getDataList()
                }
    }

    fun initRecyclerView(){
        adapter = CountryListAdapter()
        val layoutManager = LinearLayoutManager(baseActivity)
        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = adapter
        adapter!!.onItemClickListener=this // adapter内部view点击回调
    }

    private fun getDataList() {
        val param = HashMap<String, String>()
        if(!TextUtils.isEmpty(intersome.text.toString())){
            param["intersome"] = intersome.text.toString()
        }
        baseActivity?.postData("/common/getCountyCodeList",param, object : DialogCallback<ResponseBean<List<CountryListResultBean>>>(baseActivity) {
            override fun onSuccess(response: Response<ResponseBean<List<CountryListResultBean>>>) {
                setListData(response.body().data)
            }
        })
    }


    fun setListData(list: List<CountryListResultBean>) {
        adapter?.data?.clear()
        list?.let {
            adapter?.data?.addAll(list)
        }
        adapter?.notifyDataSetChanged()
        adapter?.loadMoreComplete()
        adapter?.loadMoreEnd()
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        var bean:CountryListResultBean = adapter!!.data[position] as CountryListResultBean
        if (baseActivity is RegistActivity){
            var ac: RegistActivity = baseActivity as RegistActivity
            ac.setConutryBean(bean)
            dismiss()
        }
        if (baseActivity is ChangePhoneActivity){
            var ac: ChangePhoneActivity = baseActivity as ChangePhoneActivity
            ac.setConutryBean(bean)
            dismiss()
        }

    }
    override fun onDestroy() {
        super.onDestroy()
        disposable?.dispose()
    }

    override fun onResume() {
        super.onResume()
        activity?.let {
            val manager = activity?.windowManager
            val outMetrics = DisplayMetrics()
            manager!!.defaultDisplay.getMetrics(outMetrics)
            val params = dialog.window.attributes
            params.width = outMetrics.widthPixels * 4 / 5
            dialog.window?.attributes = params
        }

    }
}